<form action="#" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label>名称:</label>
            <div class="layui-inline">
                <input type="text" name="orderName" autocomplete="off" placeholder="请输入订单名称"
                       class="layui-input">
            </div>
            状态:
            <div class="layui-inline">
                <select name="orderState">
                    <option value="">请选择</option>
                    <option value="0">取消订单</option>
                    <option value="1">已支付</option>
                    <option value="2">待支付</option>
                </select>
            </div>
            <button class="layui-btn" lay-submit lay-filter="order-table">搜索</button>
        </div>
    </div>
</form>
<script type="text/html" id="order-head-bar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="goEdit"><i class="layui-icon">&#xe654;</i>添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除
        </button>
    </div>
</script>
<script type="text/html" id="customer-customer-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit-customer">编辑</a>
</script>
<table class="layui-table" lay-filter="order-table" id="order-table">
</table>
<script type="text/javascript" >
    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        form.render();
        table.render({
            id: "order-table",
            elem: '#order-table'
            , toolbar: '#order-head-bar'
            , title: "result"
            , cellMinWidth: 80
            , url: '/sys/order/getOrder' //数据接口
            , page: true //开启分页
            , cols: [[
                {checkbox: true},
                {field: 'orderId', title: 'ID', sort: true}
                , {field: 'orderNumber', title: '订单号'}
                , {field: 'orderName', title: '订单名称'}
                , {field: 'payNumber', title: '购买数量'}
                , {field: 'startTime', title: '下单时间'}
                , {field: 'price', title: '价格'}
                , {
                    field: 'orderState', title: '状态', templet: function (data) {
                            if(data.orderState == 0){
                                return  '<span class="layui-badge layui-bg-red">取消订单</span>';
                            }else if(data.orderState == 1){
                                return  '<span class="layui-badge layui-bg-green">已支付</span>';
                            }else {
                                return  '<span class="layui-badge layui-bg-cyan">待支付</span>';
                            }


                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#customer-customer-bar', width: 180}
            ]]
        });
        //头工具栏事件
        table.on('toolbar(order-table)', function (obj) {
            switch (obj.event) {
                case 'goEdit':
                    openEditWindow(null);
                    break;
                case 'delete':
                    var data = table.checkStatus('order-table').data;
                    if (data.length > 0) {
                        layer.confirm('真的删除行么', function (index) {
                            var param = "";
                            $.each(data, function (i, obj) {
                                param += "&ids=" + obj.orderId
                            });
                            alert(param);
                            $.ajax({
                                url: '/sys/order/delOrder',
                                data: param,
                                method: 'post',
                                success: function (result) {
                                    if (result.status) {
                                        table.reload('order-table', {});
                                    } else {
                                        alert(result.message)
                                    }
                                    layer.close(index);
                                }
                            })
                        });
                    }
                    break;
            }
            ;
        ;
    });
    //监听行工具事件
    table.on('tool(order-table)', function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'edit-customer': {
                openEditWindow(data);
                break;
            }

        }
    });

    function openEditWindow(data) {
        layer.open({
            type: 1,
            content: data == null ? $('#customer-add-layer').html():$('#customer-edit-layer').html(),
            title: '编辑'
            , area: ['500px', '400px'],
            btn: ['提交', '取消'] //可以无限个按钮
            , yes: function (index, layero) {
                layer.load()
                $.ajax({
                    url: "/sys/order/" + (data == null ? 'addOrder' : 'updateOrder'),
                  //  url: '/sys/customer/update',
                    data: data == null ?$("#customer-add-form").serialize():$("#customer-edit-form").serialize(),
                    method: 'post',
                    success: function (result) {
                        if (result.status) {
                            table.reload('order-table', {});
                            layer.close(index);
                        } else {
                            alert(result.message)
                        }
                        layer.closeAll('loading');
                    }
                })
            }, success: function (layero, index) {
                form.render()
                if (data != null) {
                    form.val("customer-edit-form", data);
                    form.val("customer-edit-form", {
                        "state": data.state + ""
                    });
                }

            }
        });
    }

    form.on('submit(order-table)', function (data) {
        table.reload('order-table', {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: data.field
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    })
    ;

</script>

<script type="text/html" id="customer-edit-layer">
    <form id="customer-edit-form" style="width: 80%" class="layui-form" lay-filter="customer-edit-form">
        <input type="hidden" name="orderId">
        <div class="layui-form-item">
            <label class="layui-form-label">订单号</label>
            <div class="layui-input-block">
                <input type="text" name="orderNumber" required lay-verify="required" placeholder="请输入订单号"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">下单时间</label>
            <div class="layui-input-block">
                <input type="date" name="startTime" required lay-verify="required" placeholder="请输入下单时间"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单名称</label>
            <div class="layui-input-block">
                <input type="text" name="orderName" required lay-verify="required" placeholder="请输入订单名称"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">购买数量</label>
            <div class="layui-input-block">
                <input type="number" name="payNumber" required lay-verify="required" placeholder="请输入数量"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-block">
                <input type="number" name="price" required lay-verify="required" placeholder="请输入金额"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-block">
                <input type="radio" name="orderState" title="取消订单" value="0" />
                <input type="radio" name="orderState" title="已支付" value="1"/>
                <input type="radio" name="orderState" title="待支付" value="2" checked/>
            </div>
        </div>
    </form>
</script>



<script type="text/html" id="customer-add-layer">
    <form id="customer-add-form" style="width: 80%" class="layui-form" lay-filter="customer-edit-form">
        <input type="hidden" name="orderId">
        <input type="hidden" name="lineId" value="123">
        <div class="layui-form-item">
            <label class="layui-form-label">订单号</label>
            <div class="layui-input-block">
                <input type="text" name="orderNumber" required lay-verify="required" placeholder="请输入订单号"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">下单时间</label>
            <div class="layui-input-block">
                <input type="date" name="startTime" required lay-verify="required" placeholder="请输入下单时间"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">订单名称</label>
            <div class="layui-input-block">
                <input type="text" name="orderName" required lay-verify="required" placeholder="请输入订单名称"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">购买数量</label>
            <div class="layui-input-block">
                <input type="number" name="payNumber" required lay-verify="required" placeholder="请输入数量"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-block">
                <input type="number" name="price" required lay-verify="required" placeholder="请输入金额"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-block">
                <input type="radio" name="orderState" title="取消订单" value="0" />
                <input type="radio" name="orderState" title="已支付" value="1"/>
                <input type="radio" name="orderState" title="待支付" value="2" checked/>
            </div>
        </div>
    </form>
</script>